from django.db import models
from django.contrib.auth.models import AbstractUser
import random

VIP_Grade_CHOICE = (
    (0, "普通"),
    (1, "铜牌"),
    (2, "银牌"),
    (3, "金牌"),
    (4, "钻石")
)


class User(AbstractUser):
    VIP = models.SmallIntegerField(verbose_name='会员类型', choices=VIP_Grade_CHOICE, default=0)
    phone = models.CharField(verbose_name='手机', max_length=11, default="")
    open_id = models.CharField(verbose_name='微信小程序id', max_length=30, null=True, blank=True)
    nickname = models.CharField(verbose_name='昵称', max_length=10, default='张三')
    real_name = models.CharField(verbose_name='实名', max_length=10, default='张三')
    bank_point = models.FloatField(verbose_name='库币积分', default=110)
    inviter_number = models.IntegerField(verbose_name='邀请他们数量', default=0)
    challenge_score = models.PositiveIntegerField(default=0, verbose_name='挑战得分')
    avatar = models.TextField(default='/static/avatar/' + str(random.randint(0, 6)) + '.png', blank=True)
    is_teacher = models.BooleanField(verbose_name='是否教师', default=False)
    number = models.CharField(verbose_name='学号', max_length=8, default="")
    # avatar = models.ImageField(upload_to='avatar/%Y%m%d', verbose_name='头像图像', default='/static/uploads/avatar/4.png')

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name
        ordering = ["real_name"]


USER_APPLICATION_STATUS_CHOICE = (
    (0, "未处理"),
    (1, "同意"),
    (2, "拒绝")
)


class UserActivateApplication(models.Model):
    user = models.ForeignKey(User, related_name="UserActivateApplication", on_delete=models.CASCADE)
    add_date = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    status = models.IntegerField(verbose_name='申请状态', choices=USER_APPLICATION_STATUS_CHOICE, default=0)

    class Meta:
        verbose_name = '激活申请'
        verbose_name_plural = verbose_name
        ordering = ["-add_date", 'status']


class UserVIPApplication(models.Model):
    user = models.ForeignKey(User, related_name="UserVIPApplication", on_delete=models.CASCADE)
    add_date = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    VIP_grade = models.IntegerField(verbose_name='申请状态', choices=VIP_Grade_CHOICE, default=1)
    status = models.IntegerField(verbose_name='申请状态', choices=USER_APPLICATION_STATUS_CHOICE, default=0)

    class Meta:
        verbose_name = 'VIP申请'
        verbose_name_plural = verbose_name
        ordering = ["-add_date", 'status']


class UserNicknameApplication(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    add_date = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    nickname = models.CharField(verbose_name='昵称', max_length=10, default='张三')
    status = models.IntegerField(verbose_name='申请状态', choices=USER_APPLICATION_STATUS_CHOICE, default=0)

    class Meta:
        verbose_name = '昵称修改申请'
        verbose_name_plural = verbose_name
        ordering = ["-add_date", 'status']


NOTIFICATION_STATUS_CHOICE = (
    (0, "已读"),
    (1, '未读'),
)


class Notification(models.Model):
    user = models.ForeignKey(User, related_name="Notification", on_delete=models.CASCADE)
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
    content = models.CharField(verbose_name='通知内容', max_length=300, default='你')
    status = models.IntegerField(verbose_name='通知状态', choices=NOTIFICATION_STATUS_CHOICE, default=0)

    class Meta:
        verbose_name = '通知'
        verbose_name_plural = verbose_name
        ordering = ["-add_time", 'status']


TRANSACTION_TYPE_CHOICE = (
    (1, "下载导学案"),
    (2, '提现'),
    (3, '充值'),
    (4, '公司补充库币'),
    (5, '注册'),
    (6, '推广'),
    (7, '建议'),
    (8, '发现bug'),
    (9, '被发现bug'),
    (10, '其他'),
    (11, '刷题'),
    (12, '签到'),
    (13, '管理员直接打款'),
    (14, '观看微视频'),
    (15, "下载试卷"),
)


class CustomerBankPointChangeLog(models.Model):
    receiver = models.ForeignKey(User, related_name="BankPointChangeReceiver", on_delete=models.CASCADE, verbose_name='接受者')
    point_amount = models.FloatField(verbose_name='金额')
    remarks = models.CharField(verbose_name='备注', max_length=300)
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='交易时间')
    operator = models.ForeignKey(User, related_name="BankPointChangeSender", on_delete=models.CASCADE, verbose_name='发送者')
    transaction_type = models.PositiveIntegerField(verbose_name='交易类型', choices=TRANSACTION_TYPE_CHOICE, default=1)

    class Meta:
        verbose_name = '库币交易记录'
        verbose_name_plural = verbose_name
        ordering = ["-add_time", '-point_amount']


class CompanySupplyBankPointLog(models.Model):
    point_amount = models.PositiveIntegerField(verbose_name='金额')
    remarks = models.CharField(verbose_name='备注', max_length=300)
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='交易时间')
    operator = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='操作者')

    class Meta:
        verbose_name = '公司发放库币记录'
        verbose_name_plural = verbose_name
        ordering = ["-add_time", '-point_amount']


class Company(models.Model):
    name = models.CharField(verbose_name='公司名', max_length=20)
    president = models.CharField(verbose_name='法人用户名', max_length=20)


APPLICATION_STATUS_CHOICE = (
    (0, "未处理"),
    (1, "同意"),
    (2, "拒绝"),
    (3, "再次申请，未处理")
)


TRANSACTION_STATUS_CHOICE = (
    (1, "待支付"),
    (2, "支付成功"),
    (3, '取消')
)


class WithdrawLog(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='WithdrawLog', verbose_name='提现者')
    order_id = models.CharField(verbose_name='订单号', max_length=100)
    amount = models.PositiveIntegerField(verbose_name='库币', default=0)
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='交易时间')
    remarks = models.CharField(verbose_name='备注（如取消原因）', max_length=300, blank=True, default='')
    operator = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='操作者')
    status = models.IntegerField(verbose_name='交易状态', choices=TRANSACTION_STATUS_CHOICE, default=1)
    actual_amount = models.PositiveIntegerField(verbose_name='实际资金', default=0)

    class Meta:
        verbose_name = '提现库币记录'
        verbose_name_plural = verbose_name
        ordering = ["-add_time", 'status']


class RechargeLog(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='RechargeLog', verbose_name='充值者')
    order_id = models.CharField(verbose_name='订单号', max_length=100, blank=False)
    amount = models.PositiveIntegerField(verbose_name='金额', default=0)
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='交易时间')
    remarks = models.CharField(verbose_name='备注（提现方式，操作者等）', max_length=300)
    operator = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='操作者')
    status = models.IntegerField(verbose_name='申请状态', choices=APPLICATION_STATUS_CHOICE, default=0)
    actual_amount = models.PositiveIntegerField(verbose_name='实际资金', default=0)

    class Meta:
        verbose_name = '充值库币记录'
        verbose_name_plural = verbose_name
        ordering = ["-add_time", 'status']


class Sponsor(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Sponsor', verbose_name='赞助商')
    amount = models.PositiveIntegerField(verbose_name='金额', default=0)
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='交易时间')
    remarks = models.CharField(verbose_name='备注', max_length=300)
    publish_date = models.CharField(verbose_name='打款日期', max_length=30)

    class Meta:
        verbose_name = '赞助'
        verbose_name_plural = verbose_name
        ordering = ["-add_time", 'amount']


# 班级
class ClassGrade(models.Model):
    name = models.TextField(default='2025级1班', blank=True, verbose_name='班名')
    graduated = models.BooleanField(verbose_name='是否已毕业', default=False)
    user = models.ManyToManyField('User', through='UserOfClass', through_fields=('class_grade', 'user'))

    class Meta:
        verbose_name = '班级'
        verbose_name_plural = verbose_name
        ordering = ["graduated", 'name']

    def __str__(self):
        return self.name


# 班级和学生1对多关系
class UserOfClass(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    class_grade = models.ForeignKey(ClassGrade, on_delete=models.CASCADE)
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        verbose_name = '班级和学生关系'
        verbose_name_plural = verbose_name
        unique_together = (('user', 'class_grade'),)

    def __str__(self):
        return self.user.username + self.class_grade.name


